package com.hyg.lib_music.ui.fragment;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_common.Base.BaseApplication;
import com.hyg.lib_common.DataModel.Music.MusicCateData;
import com.hyg.lib_common.DataModel.Music.MusicDocData;
import com.hyg.lib_common.constant.UrlUtils;
import com.hyg.lib_common.WebView.WebViewActivity;
import com.hyg.lib_music.R;
import com.hyg.lib_music.adapter.MusicCateHeadViewBinder;
import com.hyg.lib_music.adapter.MusicDocViewBinder;
import com.hyg.lib_music.presenter.MusicDocListContract;
import com.hyg.lib_music.presenter.di.components.DaggerActivityComponent;
import com.hyg.lib_music.service.MusicService;
import com.hyg.lib_music.util.ApplicationComponentUtil;

import java.util.List;

import javax.inject.Inject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

import static me.drakeet.multitype.MultiTypeAsserts.assertAllRegistered;
import static me.drakeet.multitype.MultiTypeAsserts.assertHasTheSameAdapter;

public class MusicDocListFragment extends BaseFragment implements MusicDocListContract.View {

    private MusicCateData mCate;

    private List<MusicDocData> musicDocData;

    @Inject
    MusicDocListContract.Presenter presenter;

    private MultiTypeAdapter mAdapter;

    private Items items;

    private MusicService.MusicServiceBinder serviceBinder;


    public static MusicDocListFragment newInstance(MusicCateData data) {
        MusicDocListFragment fragment = new MusicDocListFragment();
        Bundle args = new Bundle();
        args.putSerializable("cate" , data);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music_system, container, false);

        RecyclerView mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setBackgroundColor(getResources().getColor(R.color.musicTabBg));
        //Dagger注入
        DaggerActivityComponent.builder()
                .applicationComponent(new ApplicationComponentUtil(BaseApplication.getInstance()).getComponent())
                .build()
                .inject(this);
        if (getArguments() != null) {
            mCate = (MusicCateData)getArguments().getSerializable("cate");
        }
        presenter.setContractView(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

        MusicCateHeadViewBinder musicCateHeadViewBinder = new MusicCateHeadViewBinder();
        musicCateHeadViewBinder.setOnMusicCateClickListener(null);
        MusicDocViewBinder musicDocViewBinder = new MusicDocViewBinder();
        musicDocViewBinder.setOnMusicDocClickListener(new MusicDocViewBinder.OnMusicDocClickListener() {
            @Override
            public void onItemClick(View view, MusicDocData musicDocData) {

                Intent it = new Intent(getActivity(), WebViewActivity.class);
                it.putExtra("url", UrlUtils.CATE_DOC+musicDocData.id+"&src=app&org=hyg");
                it.putExtra("title",musicDocData.docTitle);
                getActivity().startActivity(it);
            }

        });
//        MusicCateFooterViewBinder musicCateFooterViewBinder = new MusicCateFooterViewBinder();
//        musicCateFooterViewBinder.setOnMusicCateFooterClickListener(new MusicCateFooterViewBinder.OnMusicCateFooterClickListener() {
//            @Override
//            public void onItemClick(View view, MusicCateData musicCateData) {
//
//            }
//
//        });

        mAdapter = new MultiTypeAdapter();
        mAdapter.register(MusicCateData.class, musicCateHeadViewBinder);
        mAdapter.register(MusicDocData.class, musicDocViewBinder);

        mRecyclerView.setAdapter(mAdapter);
        assertHasTheSameAdapter(mRecyclerView, mAdapter);

        //绑定播放服务
        Intent i = new Intent(getActivity(), MusicService.class);
        getActivity().bindService(i, mServiceConnection, getActivity().BIND_AUTO_CREATE);

        presenter.getDocList(mCate.id);

        return view;
    }

    @Override
    public void setDocListData(List<MusicDocData> musicDcoDataList) {
        this.musicDocData = musicDcoDataList;
        refresh();
    }

    private void refresh() {
        //TODO 加个头
        items = new Items();
        mCate.num = this.musicDocData.size();
        items.add(mCate);
        for (int i = 0; i < this.musicDocData.size(); i++) {
            MusicDocData item = this.musicDocData.get(i);
            items.add(item);
        }
        mAdapter.setItems(items);
        mAdapter.notifyDataSetChanged();
        assertAllRegistered(mAdapter, items);
    }

    //定义与服务的连接的匿名类
    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //绑定成功后，取得MusicSercice提供的接口
            serviceBinder = (MusicService.MusicServiceBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().unbindService(mServiceConnection);
    }
}